app.controller('qusetionCtrl', ['$scope', '$http', '$filter', 'httpPlus', '$state', '$stateParams', function ($scope, $http, $filter, httpPlus, $state, $stateParams) {
    $scope.data = {
        note: '该时间段没有问卷哦!',
        noteList: [
            '该时间段没有问卷哦!',
            'success',
            '问卷已提交',
            '空订单号',
            '空订单号',
            '空订单号',
            '空订单号',
            '空订单号',
        ]
    }
    $scope.questionGroup = [];
    $scope.chooseImg_click = function (data, index) {
        console.log(data, index);
        data.point = index;
        switch (index) {
            case 0:
                data.msg = '吐槽';
                break;
            case 1:
                data.msg = '较差';
                break;
            case 2:
                data.msg = '一般';
                break;
            case 3:
                data.msg = '较好';
                break;
            case 4:
                data.msg = '很好';
                break;
        }
        data.imgSrcs.forEach(function (val, i) {
            val.src = 'img/wx/question/icon.png';
            if (i < index || i == index) {
                val.src = 'img/wx/question/blueIcon.png';
            }
            ;
        })
    };
    $scope.btnSubmit_click = function(){
        var msg = undefined;
        var answers1 = _.filter(_.map($scope.questionGroup,function(item){
            var answer  = {};
            if(item.type =='single' && item.chooseAnswer != undefined){
                answer = {
                    SurveyTitleID:item.id,
                    ChoiceText:_.filter(item.answerList,['id',item.chooseAnswer])[0].answerName,
                    Status:null
                }
            }
            return answer;
        }),function(i){
            return i.SurveyTitleID != undefined;
        });
        var answers2 = _.filter(_.map($scope.questionGroup,function(item){
            var answer2  = {};
            if(item.type =='multiple' && _.some(item.answerList,['isChoose',true])){
                answer2 = {
                    SurveyTitleID:item.id,
                    ChoiceText:_.map(_.filter(item.answerList,['isChoose',true]),'answerName').join(','),
                    Status:null
                }
            }
            return answer2;

        }),function(i){
            return i.SurveyTitleID != undefined;
        });
        var answers3 = _.filter(_.map($scope.questionGroup,function(item){
            var answer3  = {};
            if(item.type =='point' && item.point != undefined){
                answer3 = {
                    SurveyTitleID:item.id,
                    ChoiceText:item.point.toString(),
                    Status:null
                }
            }
            return answer3;

        }),function(i){
            return i.SurveyTitleID != undefined;
        });
        var answers4 = _.filter(_.map($scope.questionGroup,function(item){
            var answer4  = {};
            if(item.type =='text'){
                answer4 = {
                    SurveyTitleID:item.id,
                    ChoiceText:item.msg,
                    Status:null
                }
            }
            return answer4;

        }),function(i){
            return i.SurveyTitleID != undefined;
        });
        if(_.concat(answers1,answers2,answers3).length ==_.filter($scope.questionGroup,function(i){return i.type!='text'}).length){
            httpPlus
                .MMB2RRRApi('Manage/MemberSurvey/Survey_MemberSubmit',{
                    SurveyGroupID:$scope.groupId,
                    BillNo:$stateParams.billno,
                    answers:_.concat(answers1,answers2,answers3,answers4),
                })
                .then(function(data){
                    $state.go('wx.submitResult',{type:1});
                },function(err){
                    alert(err.ResultMsg);
                    $state.go('wx.submitResult',{type:0});
                })
                }else {
                    alert('还有题目没有选择哦');
                }



    };
    httpPlus
        .MMB2RRRApi('Manage/MemberSurvey/SurveyGroup_GetCurrentByBillNo',{
            BillNo:$stateParams.billno
        })
        .then(function (data) {
            if (data.status == 1) {
                $scope.groupId = data.group.SurveyGroupID;
                $scope.qusetionTitle = data.group.Title;
                $scope.questionGroup = _.map(data.titleList, function (item) {
                    return {
                        id: item.SurveyTitleID,
                        groupName: item.title.Survey_Title,
                        type: item.title.Type,
                        answerList: _.map(item.answers, function (v) {
                            return {
                                id: v.SurveyAnswersID,
                                answerName: v.ChoiceText
                            }
                        })
                    }
                });
                console.log($scope.questionGroup);
                 $scope.questionGroup.forEach(function (item) {
                        item.point = undefined;
                       item.msg = undefined;
                       if (item.type == 'single') {
                           item.chooseAnswer = undefined;
                       }
                       if (item.type == 'multiple') {
                           _.forEach(item.answerList,function(a){
                               a.isChoose = false;
                           });
                       }
                       if (item.type == 'point') {
                           item.imgSrcs = _.map(_.range(1, 6), function (val) {
                               return {
                                   id: _.padStart(val, 3, 0),
                                   src: 'img/wx/question/icon.png'
                               }
                           });
                       }
                   })
            } else {
                $scope.data.note = $scope.data.noteList[data.status];
            }
        }, function (err) {
            //console.log(err.ResultMsg);
            alert(err.ResultMsg);
        })
}])